let Strategy = {
	/**
	 * @param {Object} preYearData
	 * @param {Object} nowYearData
	 * 计算单前值
	 */
	cul: function(code, nowDateStart, nowDateEnd, preDateStart, preDateEnd, callBack) {
		let dayDao = GuVo.getDateRang(code, nowDateStart, nowDateEnd);
		let preYearData = GuVo.getDateRang(code, preDateStart, preDateEnd);
		let preYearMonth = getLastMonth2(code);
       let today = GuVo.getNowP(code);
		Promise.all([dayDao, preYearData, preYearMonth,today]).then(res => {
			let nowYearDatas = res[0].data[code].day;
			let todayP =GuVo.getNowVo(code,res[3]);
			let date = new Date().Format("yyyy-MM-dd");
			if(date!=nowYearDatas[nowYearDatas.length-1][0]){
				nowYearDatas.push([date,todayP.openPrice,todayP.price,todayP.maxPrice,todayP.minPrice]);
			}
			//nowYearDatas.push([,todayP.openPrice,todayP.nowPrice,todayP.maxPrice,todayP.minPrice]);
			let nowYearCompare = this.compareNowYear(res[0].data[code].day);
			let nowYearResult = this.getMinMaxAvg(res[0].data[code].day,3,4); //今年数据
			let preYearResult = this.getMinMaxAvg(res[1].data[code].day,3,4); //上年数据
			let endYear = nowDateStart.substr(0, 4);
			endYear = parseInt(endYear);
			let name = res[0].data[code].qt[code][1];
			
			this.compareEveryYear(res[2], code, endYear, ((res,allYear,allMonth) => {
				let list = [];
				let yearArr = res; //每年最高最低
				let history =  this.getMinMaxAvg(yearArr,"max","min"); 
				//this.computResult(nowYearCompare,preYearResult,yearArr);
			
				list.push({
					propCN: "名称",
					val: name
				}, {
					propCN: "今年最高",
					val: nowYearResult.max
				});
				list.push({
					propCN: "今年最高时间",
					val: nowYearResult.maxTime
				});
				list.push({
					propCN: "今年最低",
					val: nowYearResult.min
				});
				list.push({
					propCN: "今年最低时间",
					val: nowYearResult.minTime
				}, {
					propCN: "上年最高",
					val: preYearResult.max
				});
				list.push({
					propCN: "上年最高时间",
					val: preYearResult.maxTime
				});
				list.push({
					propCN: "上年最低",
					val: preYearResult.min
				});
			list.push({
					propCN: "上年最低时间",
					val: preYearResult.minTime
				});	list.push({
						propCN: "历年最大值",
						val: history.max
					});	list.push({
						propCN: "历年最小值",
						val: history.min
					});
					
					yearArr.map(res=>{
					list.push(
					{propCN:res.maxTime+"最大值",val:res.max}
					);	list.push(
						{propCN:res.minTime+"最小值",val:res.min}
						);
					});
					list.push(
					{propCN:nowYearResult.maxTime+"最大值",val:nowYearResult.max}
					);
					list.push({propCN:nowYearResult.minTime+"最小值",val:nowYearResult.min});
					allYear[nowYearDatas[0][0].substr(0,4)] = nowYearDatas;//今年最大最小值
				callBack(list,allYear,allMonth);

			}));

		});
	},

	//和当年做比较
	compareNowYear: function(arr) {
		let nowPrice = parseFloat(arr[arr.length - 1][1]);
		let result = this.getMinMaxAvg(arr, 2, 3);
		result.nowPrice = nowPrice;
		return result;
	},

	//统计每年
	compareEveryYear: function(res, code, endYear, callBack) {

		var arr = res.split("\n");
		arr.pop();
		arr.shift();
		arr = arr.map(row => {
			return row.split(" ");
		})
		let year = arr[0][0].substr(0, 2);
		let year1 = year.substr(0, 1);
		if (year1 == "0" || year1 == "1" || year1 == "2") {
			year = "20" + year;
		} else {
			year = "19" + year;
		}
		year = parseInt(year);
		let pList = [];
		for (let i = year; i < endYear; i++) {
			pList.push(GuVo.getDateRang(code, year + "-01-01", year + "-12-31"));
			year++;
		}
		let yearArr = [];
		let allYear = {};
		let allMonth={};
		Promise.all(pList).then(res => {
			res.map(item => {
				let yearData = item.data[code].day;
				let result = this.getMinMaxAvg(yearData, 2, 3);
				allYear[yearData[0][0].substr(0,4)] = yearData;
				let m = this.computeYearMonth(yearData);
				for(let i=0;i<12;i++){
					 let mh=(i+1)<10?"m0"+(i+1):"m"+(i+1);
						  
				      if(typeof m[mh] == "boolean"){
						  m[mh]?(allMonth["r"+mh]?allMonth["r"+mh]++:allMonth["r"+mh]=1):(allMonth["g"+mh]?allMonth["g"+mh]++:allMonth["g"+mh]=1);
					  }
				}
				yearArr.push(result);
			});
			//debugger;
			console.log(allMonth);
			callBack(yearArr,allYear,allMonth);
		});

	},
		//统计每年月红绿
	computeYearMonth:function(arr){
		let month={};	
	    arr.map(item=>{
			let temp = parseFloat(item[2]);
			if(month["p"+item[0].substr(5,2)]){
				month["m"+item[0].substr(5,2)]=month["p"+item[0].substr(5,2)]<temp;
			}else{
				month["p"+item[0].substr(5,2)]=temp;
			}
		});
		return month;
	},
	
	//计算买卖数值
	computResult: function(now, pre, every) {
		let buy = buyScore(now, pre, every);
		let sell = sellScore(now, pre, every);
		if (app.culBuySell) {
			app.culBuySell({
				buy: buy,
				sell: sell,
				code: now.code
			});
		}
		console.log(buy + ":" + sell)
	},
	//avg,price,max
	computPersentSell: function(a, b, c) {
		let p = (b - a) / (c - a);
		p = p > 1 ? 1 : (p < 0 ? 0 : p);
		return p;
	},
	//min,price,avg
	computPersentBuy: function(a, b, c) {
		
		let p = (c - b) / (c - a);
		p = p > 1 ? 1 : (p < 0 ? 0 : p);
		return p;
	},
	/**
	 * @param {Object} now
	 * @param {Object} pre
	 * @param {Object} every
	 * 数值=当年靠近最小值比率*40+上年靠近最小值比率*30+当月占每年低位月份比率*30
	 */
	buyScore: function(now, pre, every) {
		let nowPrice = now.price;
		let pNow = this.computPersentBuy(now.min, nowPrice, now.avg);
		let pPre = this.computPersentBuy(pre.min, nowPrice, pre.avg);
		let score = pNow * 40 + pPre * 30 + every.minPersent * 30;
		return score;

	},
	/**
	 * @param {Object} now
	 * @param {Object} pre
	 * @param {Object} every
	 * 计算卖出公式
	 * 数值=当年靠近最大值比率*40+上年靠近最大值比率*30+当月占每年高位月份比率*30
	 */
	sellScore: function(now, pre, every) {
		let nowPrice = now.price;
		let pNow = this.computPersentSell(now.avg, nowPrice, now.max);
		let pPre = this.computPersentSell(pre.avg, nowPrice, pre.max);
		let score = pNow * 40 + pPre * 30 + every.maxPersent * 30;
		return score;
	},

	//获取最大最小平均值
	getMinMaxAvg: function(arr, highIdx, lowIdx) {
		let max = 0;
		let min = 99999999;
		let avg = 0;
		let maxTime = "";
		let minTime = "";
		arr.forEach(res => {
			let r3 = parseFloat(res[highIdx]);
			let r4 = parseFloat(res[lowIdx]);
			max = r3 > max ? r3 : max;
			min = r4 < min ? r4 : min;
			maxTime = max == r3 ? res[0] : maxTime;
			minTime = min == r4 ? res[0] : minTime;
		})
		max = parseFloat(max);
		min = parseFloat(min);
		avg = (max + min) * 0.5;
		avg = parseFloat(avg.toFixed(2));
		return {
			maxTime: maxTime,
			minTime: minTime,
			max: max,
			min: min,
			avg: avg
		};
	}

	///
}
